import pandas as pd
import numpy as np


# 推荐的显示配置
def set_pandas_display():
    pd.set_option('display.max_rows', 100)
    pd.set_option('display.max_columns', 50)
    pd.set_option('display.width', 1000)
    pd.set_option('display.max_colwidth', 50)
    pd.set_option('display.precision', 2)


set_pandas_display()


df = pd.read_csv("../../data/pd_data/sleep.csv")
print(df.info())
print(df.describe())
print(df.head())
print(df.isna().sum())

print(df['sleep_disorder'].value_counts())

# 缺失值处理1：删除该列
# df.drop(columns='sleep_disorder', inplace=True)
# print(df.isna().sum())

# 缺失值处理2：为列缺失值设置默认值
# df['sleep_disorder'].fillna('Unknown', inplace=True)
df.fillna({'sleep_disorder': 'Unknown'}, inplace=True)
print(df.isna().sum())
print(df['sleep_disorder'].value_counts())


df['gender'] = df['gender'].astype('category')
print(df['occupation'].value_counts())
df['occupation'] = df['occupation'].astype('category')
print(df['bmi_category'].value_counts())
df['bmi_category'] = df['bmi_category'].astype('category')

df[['high', 'low']] = df['blood_pressure'].str.split('/', expand=True)

# 睡眠质量分箱
labels = ['差', '中', '优']
df['sleep_quality_level'] = pd.cut(df['sleep_quality'], bins=3, labels=labels)

age_labels = ['青少年', '中年', '老年']
df['age_level'] = pd.cut(df['age'], bins=[0, 24, 45, 100], labels=age_labels)

print(df.info())
print(df.head())


# 根据不同的bmi类别分组，分析睡眠质量
print(df['bmi_category'].value_counts())

print(df.groupby('bmi_category', observed=True).agg({
    'sleep_duration': 'mean',
    'sleep_quality': 'mean',
    'stress_level': 'mean',
    'heart_rate': 'mean',
    'physical_activity_level': 'mean',
}))

